/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * ByteArray.java
 * Created on March 1, 2008, 11:20 AM
 */

package dicterator.dict.mobile;

/**
 *
 * @author Erol Hira
 */
public class ByteArray {
    
    private byte[] bytes;
    private int offset;
    private int mark;
    
    public ByteArray(byte[] bytes) {
        this.bytes = bytes;
    }
    
    //---------------------------------------------------------------------
    
    public int readInt(int offset) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (3 - i) * 8;
            value += (bytes[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }
    
    public int readInt() {
        int i = readInt(offset);
        offset += 4;
        return i;
    }
    
    //---------------------------------------------------------------------
    
    public short readShort(int offset) {
        short value = 0;
        for (int i = 0; i < 2; i++) {
            short shift = (short)((1 - i) * 8);
            value += (bytes[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }        
    
    public short readShort() {
        short s = readShort(offset);
        offset += 2;
        return s;
    }
    
    //---------------------------------------------------------------------
    
    public byte readByte(int offset){
        return bytes[offset];
    }
    
    public byte readByte(){
        byte b = bytes[offset];
        offset ++;
        return b;
    }
    
    //---------------------------------------------------------------------
    
    public String readUTF(int offset){
        byte length = bytes[offset];        
        offset ++;
        byte[] bs = copyArray(bytes, offset, length);
        return Utils.bytes2string(bs);
    }
    
    public String readUTF(){
        String str = readUTF(offset);
        byte length = bytes[offset];
        offset += 1 + length;
        return str;
    }
    
    //---------------------------------------------------------------------
    
    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
    
    //---------------------------------------------------------------------
    
    public void mark(){
        mark = offset;
    }
    
    public void reset(){
        offset = mark;
    }
    
    public void skip(int length){
        offset += length;
    }
    
    //---------------------------------------------------------------------
    
    public static byte[] copyArray(byte[] bs, int offset, int length){
        byte[] bytes = new byte[length];
        for(int i = 0; i < length; i++){
            bytes[i] = bs[offset + i];
        }
        
        return bytes;
    }
}
